<?php
class CommentsController extends AppController {
	public $name = 'Comments';
	public $uses=array('Comment','Task','TaskNotification','FileManager','FileAttach');
	
	
	public function admin_add($id=0){
				
		$taskid=intval($id);		
		if($taskid){			
			$user=$this->Auth->user();
			if(!empty($this->data)){
				$data=$this->data;
				$data[$this->uses[0]]['task_id']=$taskid;
				$data[$this->uses[0]]['user_id']=$user['id'];
				
				
				//add attachment
				//insert file attach
				$updateMsg='';
				if(isset($data['FileAttach'])){
					//t($data['Field']);
					foreach($data['FileAttach'] as $k=>$v){
						if(!empty($v['id'])){
							$insertdata['user_id']=$user['id'];
							$insertdata['file_id']=$v['id'];
							$insertdata['task_id']=$taskid;		
							$this->FileAttach->create($insertdata);
							$this->FileAttach->save();
							//mount to file
							$this->FileManager->updateAll(array('mount'=>'mount + 1'),array('id'=>$v['id']));
							$updateMsg.='&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;'.$v['name'].'<br />';
						}
					}
				}
				if(!empty($updateMsg))$updateMsg='<b>Task update:</b><br/>Attach new file:<br />'.$updateMsg;
				$this->Comment->addComment($updateMsg,$user['id'],$data[$this->uses[0]]['task_id']);
				
				$this->Comment->addComment($data[$this->uses[0]]['comment'],$user['id'],$data[$this->uses[0]]['task_id']);
				
				
			}
		}
		$this->admin_lists($taskid);
		//set bien locale ra view de su dung
		
		
	}

	function admin_lists($id=0){
		//get comment
		$id=intval($id);
		$data=$this->{$this->uses[0]}->find('all',array('conditions'=>'Task_id = '.$id));
		$this->set('data',$data);
		$this->render('admin_lists');
	}

}
?>
